
<?php

session_start();

if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){

require_once('../config.inc.php');
require_once('../../classes/Mypdo.class.php');
require_once('../../classes/ProprietaireManager.class.php');
require_once('../../classes/PersonneManager.class.php');
require_once('../../classes/LocationManager.class.php');
require_once('../../classes/ChargeManager.class.php');
require_once('../../classes/Proprietaire.class.php');
require_once('../../classes/Personne.class.php');
require_once('../../classes/Location.class.php');
require_once('../../classes/LocationRegle.class.php');
require_once('../../classes/Charge.class.php');
require_once('../../classes/Adresse.class.php');
require_once('../../classes/Loyer.class.php');
require_once('../../classes/LoyerManager.class.php');
require_once('../../classes/Locataire.class.php');
require_once('../../classes/LocataireManager.class.php');
require_once('../../classes/Immeuble.class.php');
require_once('../../classes/ImmeubleManager.class.php');

define('FPDF_FONTPATH',$_SERVER['DOCUMENT_ROOT'].'/image/fonts/');
require('../../classes/Fpdf.class.php');
//var_dump($_SESSION);
//var_dump($_POST);

$db = new Mypdo();
if($_GET['id']){
    $loyerManager = new LoyerManager($db);
    $locManager = new LocationManager($db);
    $locaManager = new LocataireManager($db);
    $chargeManager = new ChargeManager($db);
    $immeuManager = new ImmeubleManager($db);


    $listeLoyer = $loyerManager->getListByIdLoca($_GET['id']);
    $header = array('Date','Location','A Payer','Payer','Remarque','Statut');
    $data = array();
    foreach ($listeLoyer as $value) {

        $row = array();

        $row[] = $value->getDateLoyer();

        $loc = $locManager->getLoc($value->getIdLoc());
        $pers = $locaManager->getPersonne($_GET['id']);


        if(gettype($loc) == 'object'){
            $immeu = $immeuManager->getImmeu($loc->getIdImmeu());
            $row[] = utf8_decode($loc->getNomLoc())/*."\n".$immeu->getAdresseImmeu()*/;
        }else{
            $locs = '';
            foreach ($loc as $val) {
                $immeu = $immeuManager->getImmeu($val->getIdImmeu());

                $locs = $locs.$locs != ''?"\n":''.$val->getNomLoc()/*."\n".$immeu->getAdresseImmeu()*/;
            }
            $row[] = utf8_decode($locs);
        }

/*        if(gettype($pers) == 'object'){
            $row[] = $pers->getNomPers().' '.$pers->getPrenomPers();
        }else{
            $perss = '';
            foreach ($pers as $val) {
             $perss = $perss.($perss != ''?"\n":'').$val->getNomPers().' '.$val->getPrenomPers();
            }
            $row[] = $perss;
        }
*/
        $listeCharge = $chargeManager->getListByIdLoc($value->getIdLoc());
        $brutAPayer = $loc->getMontantLoc();
        $netAPayer = $brutAPayer + $brutAPayer*($loc->getTvaLoc()/100);

        foreach ($listeCharge as $valeur) {
            $montant = $valeur->getMontantCharge();

            $tva =  ($valeur->getTvaCharge()/100);
            $charge = ($montant + ($montant * $tva ));
            $netAPayer = $netAPayer+ $charge;
        }
        $row[] = $netAPayer;

        $row[] = $value->getSoldePayerLoyer();

        $row[] = utf8_decode($value->getRemarqueLoyer());

        $row[] = $value->getStatutLoyer() == 0?'Impayer': 'Acquitter';

        $data[] = $row;
    }





    $pdf = new FPDF('L','mm','A4');
    $pdf->SetFont('Arial','',14);

    $pdf->AddPage();
    //var_dump($header);
    //var_dump($data);
    $pdf->FancyTable($header,$data);
    $pdf->Output();
}
}else{
    header('Location: index.php');
}
//session_destroy();
